import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_house_manager/pages/home/index.dart';
import 'package:flutter_house_manager/pages/mine/index.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super.key});

  @override
  State<StatefulWidget> createState() => _TabsPageState();
}

// ignore: unused_element
List tabList = [
  {
    "label": '首页',
    "icon": "assets/images/home.svg",
    "active_icon": "assets/images/home.svg"
  },
  {
    "label": '我的',
    "icon": "assets/images/home.svg",
    "active_icon": "assets/images/home.svg"
  }
];
List<BottomNavigationBarItem> getTabsBar() {
  // 首页的tabs 和我的tabs
  List<BottomNavigationBarItem> tabsList = [];
  // push pop unshift shift
  for (var i = 0; i < tabList.length; i++) {
    tabsList.add(
      BottomNavigationBarItem(
          icon: SvgPicture.asset(tabList[i]["icon"], width: 30, height: 30),
          activeIcon: SvgPicture.asset(tabList[i]["active_icon"],
              width: 30, height: 30),
          label: tabList[i]["label"]),
    );
  }
  return tabsList;
}

int _currentIndex = 0;

class _TabsPageState extends State<TabsPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            child: IndexedStack(
          index: _currentIndex, // 0 => 渲染第一个组件 1 => 第二个组件
          children: const [
            HomePage(),
            MinePage(),
          ],
        )),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            _currentIndex = index; // 数据变化了 但是UI没有更新
            setState(() {});
          },
          items: getTabsBar(),
          // 底部导航栏
        ));
  }
}
